package gnt.sd;

import gnt.sd.controller.Service;
import gnt.sd.controller.ServiceListener;
import gnt.sd.controller.ServiceRespone;
import gnt.sd.model.YoutubeData;
import gnt.sd.view.SDYoutubeAdapter;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class SDYoutubeActivity extends Activity implements ServiceListener {
	ArrayList<YoutubeData> _listYoutube;
	SDYoutubeAdapter _listAdapter;
	ListView _listView;
	TextView _headerText;
	EditText _searchBar;
	Service _serviceSearch;
	String _keyword;
	String _showSearchBar;
	ProgressDialog _dialog;

	@SuppressWarnings("unchecked")
	public void onCreate(Bundle savedInstanceState) {
		// Log Ranking
		super.onCreate(savedInstanceState);
		setContentView(R.layout.youtube_view);
		_serviceSearch = new Service(this);
		_listView = (ListView) findViewById(R.id.youtube_list);
		SDApplication mobionApp = (SDApplication) getApplication();
		View header = getLayoutInflater().inflate(
				R.layout.youtube_searchheader, null);
		_headerText = (TextView) header.findViewById(R.id.youtube_headerText);
		_searchBar = (EditText) header.findViewById(R.id.youtube_search);
		_searchBar
				.setOnEditorActionListener(new TextView.OnEditorActionListener() {
					public boolean onEditorAction(TextView v, int actionId,
							KeyEvent event) {
						if (actionId == EditorInfo.IME_ACTION_SEARCH) {
							_headerText.setText("Results for: "
									+ _searchBar.getText().toString());
							_dialog = ProgressDialog.show(
									SDYoutubeActivity.this, "Please wait...",
									"Searching...!", true);
							_serviceSearch.searchYoutube(_searchBar.getText()
									.toString());
						}
						return false;
					}
				});
		_listView.addHeaderView(header);
		_listYoutube = (ArrayList<YoutubeData>) mobionApp
				.getData("listYoutube");
		_listAdapter = new SDYoutubeAdapter(_listView, this,
				R.layout.youtube_tablecell, _listYoutube);
		_listView.setAdapter(_listAdapter);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void onComplete(Service service, ServiceRespone result) {
		// TODO Auto-generated method stub
		if (result.isSuccess()) {
			_listYoutube = (ArrayList<YoutubeData>) result.getData();
			_listAdapter = new SDYoutubeAdapter(_listView, this,
					R.layout.youtube_tablecell, _listYoutube);
			_listView.setAdapter(_listAdapter);
		}
		_dialog.dismiss();
	}

}
